import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../store/auth';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/HomeView.vue')
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/user',
      name: 'User',
      component: () => import('@/views/UserView.vue'),
      meta: {
        requiresAuth: true
      }
    },
    {
      path: '/assessment',
      name: 'AssessmentChat',
      component: () => import('../views/AssessmentChat.vue'),
      meta: {
        keepAlive: true,
        requiresAuth: true
      }
    }
  ]
})

// 添加全局路由守卫
router.beforeEach(async (to, from, next) => {
  console.log(from)
  const authStore = useAuthStore()
  if (to.meta.requiresAuth && !authStore.token) {
    next({ name: 'login' })
  } else {
    next()
  }
})

export default router